<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>规格管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
    
</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->                
                    <div class="box-header with-border">
                        <h3 class="box-title">规格管理</h3>
                    </div>
                    <div class="box-body">
                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#addModal"><i class="fa fa-trash-o"></i> 新建</button>
                                        <input type="button" onclick="deleteBatch()" value="删除" class="btn btn-default" title="删除" />
                                        
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
							<form id="specificationNameForm">
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                    规格名称：<input type="text" name="specName" >
									<input type="button" class="btn btn-default" value="查询" onclick="getSpecificationList()">
                                </div>
                            </div>
							</form>
                            <!--工具栏/-->
			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">规格ID</th>
									      <th class="sorting">规格名称</th>									     												
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="specificationContent">

			                      </tbody>
			                  </table>
			                  <!--数据列表/-->    
                        </div>
                        <!-- 数据表格 /--> 
                     </div>
                    <!-- /.box-body -->

  <!-- 增加窗口 -->
  <div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="addMyModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="addMyModalLabel">规格编辑</h3>
			  </div>
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px">
					  <tr>
						  <td>规格名称</td>
						  <td><input id="addSpecName" class="form-control" placeholder="规格名称" >  </td>
					  </tr>
				  </table>

				  <!-- 规格选项 -->
				  <div class="btn-group">
					  <input type="button" onclick="newAddSpecificationOption()" class="btn btn-default" title="新建" value="新增规格选项" />

				  </div>

				  <table id="addSpecOption" class="table table-bordered table-striped table-hover dataTable">
					  <thead>
					  <tr>
						  <th class="sorting">规格选项</th>
						  <th class="sorting">排序</th>
						  <th class="sorting">操作</th>
					  </thead>
					  <tbody id="addSpecificationOption">
					  <tr id="1">

						  <td>
							  <input name="optionName"  class="form-control" placeholder="规格选项">
						  </td>
						  <td>
							  <input name="orders" class="form-control" placeholder="排序">
						  </td>
						  <td>
							  <input type="button" onclick='deleteAddSpecificationOption("1")' class="btn btn-default" title="删除" value="删除"/>
						  </td>
					  </tr>
					  <tr id="2">

						  <td>
							  <input name="optionName" class="form-control" placeholder="规格选项">
						  </td>
						  <td>
							  <input name="orders" class="form-control" placeholder="排序">
						  </td>
						  <td>
							  <input type="button" onclick='deleteAddSpecificationOption("2")' class="btn btn-default" title="删除" value="删除"/>
						  </td>
					  </tr>
					  <tr id="3">

						  <td>
							  <input name="optionName"  class="form-control" placeholder="规格选项">
						  </td>
						  <td>
							  <input name="orders"  class="form-control" placeholder="排序">
						  </td>
						  <td>
							  <input type="button" onclick='deleteAddSpecificationOption("3")' class="btn btn-default" title="删除" value="删除"/>
						  </td>
					  </tr>
					  </tbody>
				  </table>


			  </div>
			  <div class="modal-footer">
				  <input type="button" onclick="add()" value="保存" class="btn btn-success" data-dismiss="modal" aria-hidden="true"/>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>

  <!-- 编辑窗口 -->
  <form id="updateForm">
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">规格编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table id="updateSpecification" class="table table-bordered table-striped"  width="800px">

			 </table>				
			 
			 <!-- 规格选项 -->
			 <div class="btn-group">
                  <input type="button" onclick="newUpdateSpecificationOption()" class="btn btn-default" title="新建" value="新增规格选项" />
                  
             </div>
			 
			 <table class="table table-bordered table-striped table-hover dataTable">
                    <thead>
                        <tr>
                          
					 
				      <th class="sorting">规格选项</th>
				      <th class="sorting">排序</th>																
                      <th class="sorting">操作</th>	
                    </thead>
                    <tbody id="updateSpecificationOption">
					  <tr>
                           
				            <td>
				            	<input  class="form-control" placeholder="规格选项"> 
				            </td>
				            <td>
				            	<input  class="form-control" placeholder="排序"> 
				            </td>
							<td>
								<button type="button" class="btn btn-default" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>
							</td>
                      </tr>
                    </tbody>
			  </table> 
			
			
		</div>
		<div class="modal-footer">						
			<input type="button" onclick="update()" value="保存" class="btn btn-success" data-dismiss="modal" aria-hidden="true"/>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div></form>

<script>

	$(function (){
		getSpecificationList()
	})

	function getSpecificationList(){
		$.ajax({
			url:"/specificationController/getSpecificationList",
			data:$("#specificationNameForm").serialize(),
			type:"get",
			dataType:"json",
			async:false,
			success:function(result){
				var list = result.result;
				var dd = '';
				for(i in list){
					dd += "<tr >"
					dd += "<td><input  type=\"checkbox\" name='specification' value='" + list[i].id + "' ></td>"
					dd += "<td>" + list[i].id + "</td>"
					dd += "<td>" + list[i].specName + "</td>"
					dd += "<td class=\"text-center\">"
					dd += "<input type=\"button\" onclick='toUpdate(" + list[i].id + ")' class=\"btn bg-olive btn-xs\" data-toggle=\"modal\" data-target=\"#editModal\" value='修改'/>"
					dd += "</td>"
					dd += "</tr>"
				}
				$("#specificationContent").html(dd)
			},
			error:function(){
				alert("系统错误!")
			}
		})
	}

	function add(){

		var specName = $("#addSpecName").val()

		var trs = $("#addSpecOption").find("tr")
		var optionList = []
		for (var i = 1; i < trs.length; i++){
			var tds = $(trs[i]).find("td")
			var option = {id:null, optionName:$(tds[0]).find("input").val(), orders:$(tds[1]).find("input").val(), specId:null}
			optionList.push(option)
		}
		var specOptionListStr = JSON.stringify(optionList)

		$.ajax({
			url:"/specificationController/addSpecification",
			data:{"specName" : specName, "specOptionListStr" : specOptionListStr},
			dataType:"json",
			type:"get",
			async:false,
			success:function (result){
				var msg = result.message
				window.location.reload();
				alert(msg)
			},
			error:function (){
				alert("系统错误!")
			}
		})
	}

	function toUpdate(id){
		$.ajax({
			url:"/specificationController/toUpdate/" + id,
			dataType:"json",
			type:"get",
			async:false,
			success:function (result){
				var list = result.result
				console.log(list)
				var dd = ''
				for(i in list){
					dd += "<tr id='" + "-" + list[i].orders + "'>"
					dd += "<td>"
					dd += "<input name=\"optionName\" value='" + list[i].optionName + "' class=\"form-control\" placeholder=\"规格选项\">"
					dd += "</td>"
					dd += "<td>"
					dd += "<input name=\"orders\" value='" + list[i].orders + "' class=\"form-control\" placeholder=\"排序\">"
					dd += "</td>"
					dd += "<td>"
					dd += "<input type=\"button\" onclick='deleteUpdateSpecificationOption(" + "-" + list[i].orders + ")' class=\"btn btn-default\" title=\"删除\" value='删除'/>"
					dd += "</td>"
					dd += "</tr>"
				}
				$("#updateSpecificationOption").html(dd)
				dd = '<tr>' +
						'<td>规格名称</td>' +
						'<input type="hidden" id="updateId" value="' + list[0].id + '">' +
						'<td id=""><input id="updateSpecName" value="' + list[0].specName + '"  class="form-control" placeholder="规格名称" >  </td>' +
						'</tr>'
				$("#updateSpecification").html(dd)
			},
			error:function (){
				alert("系统错误!")
			}
		})
	}

	function update(){

		var id = $("#updateId").val()
		var specName = $("#updateSpecName").val()

		var trs = $("#updateSpecificationOption").find("tr")
		var optionList = []
		for (var i = 0; i < trs.length; i++){
			var tds = $(trs[i]).find("td")
			var option = {id:null, optionName:$(tds[0]).find("input").val(), orders:$(tds[1]).find("input").val(), specId:null}
			optionList.push(option)
		}
		var specOptionListStr = JSON.stringify(optionList)

		$.ajax({
			url:"/specificationController/updateSpecification",
			data:{"id" : id, "specName" : specName, "specOptionListStr" : specOptionListStr},
			dataType:"json",
			type:"post",
			async:false,
			success:function (result){
				var msg = result.message
				window.location.reload();
				alert(msg)
			},
			error:function (){
				alert("系统错误")
			}
		})
	}

	function deleteBatch(){
		var allBrand = document.getElementsByName("specification")
		var checked = []
		for(i in allBrand){
			if(allBrand[i].checked){
				checked.push(allBrand[i].value)
			}
		}

		if(checked.length <= 0){
			alert("请选择品牌!");
			return
		}

		console.log(checked)

		$.ajax({
			url:"/specificationController/deleteBatch",
			traditional: true,
			data:{"ids" : checked},
			dataType:"json",
			type:"get",
			async:false,
			success:function (result){
				var msg = result.message
				window.location.reload()
				alert(msg)
			},
			error:function (){
				alert("系统错误!")
			}
		})
	}

	var addOptionId = 4
	function newAddSpecificationOption(){
		var dd = "<tr id='" + addOptionId + "'>" +
				"<td>" +
				"<input name=\"optionName\"  class=\"form-control\" placeholder=\"规格选项\">" +
				"</td>" +
				"<td>" +
				"<input name=\"orders\"  class=\"form-control\" placeholder=\"排序\">" +
				"</td>" +
				"<td>" +
				"<input type=\"button\" onclick='deleteAddSpecificationOption(" + addOptionId + ")' class=\"btn btn-default\" title=\"删除\" value='删除'/>" +
				"</td>" +
				"</tr>";
		addOptionId += 1
		$("#addSpecificationOption").append(dd)
	}

	function deleteAddSpecificationOption(id){
		$("#" + id).html('')
	}

	function newUpdateSpecificationOption(){
		var dd = "<tr>" +
				"<td>" +
				"<input name=\"optionName\"    class=\"form-control\" placeholder=\"规格选项\">" +
				"</td>" +
				"<td>" +
				"<input name=\"orders\"    class=\"form-control\" placeholder=\"排序\">" +
				"</td>" +
				"<td>" +
				"<input type=\"button\" onclick='deleteUpdateSpecificationOption(" + addOptionId + ")' class=\"btn btn-default\" title=\"删除\" value='删除'/>" +
				"</td>" +
				"</tr>"
		$("#updateSpecificationOption").append(dd)
	}

	function deleteUpdateSpecificationOption(id){
		$("#" + id).html('')
	}

</script>

</body>

</html>